package com.centaline.androidsalesblog.activities.search;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.activities.sale.EsfEstateDetailActivity;
import com.centaline.androidsalesblog.api.sale.AutoMatchApi;
import com.centaline.androidsalesblog.base.BaseSearchActivity;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.constants.DefaultHint;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.model.businissobject.AutoMatchBo;
import com.centaline.androidsalesblog.model.databaseobject.SearchDo;
import com.centaline.androidsalesblog.model.responseobject.AutoMatchRo;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.ArrayList;

/**
 * 二手房搜索[租售、小区]
 * Created by guilin on 16/5/4.
 */
public class SaleSearchActivity extends BaseSearchActivity {

    private AutoMatchApi autoMatchApi;

    @Override
    protected void initViews() {
        super.initViews();
        type = getIntent().getIntExtra(ExtraConstant.SearchType, 20);

        autoMatchApi = new AutoMatchApi(SaleSearchActivity.class, AutoMatchRo.class,
                new IResponseListener<AutoMatchRo>() {
                    @Override
                    public void onResponse(AutoMatchRo autoMatchRo) {
                        final ArrayList<AutoMatchBo> temp = autoMatchRo.getList();
                        searchList.clear();
                        if (temp == null || temp.size() == 0) {
                            if (fromUser) {
                                snack("搜索结果为空");
                            } else {
                                rv_search.setVisibility(View.GONE);
                                showHistoryLayout(true);
                            }
                        } else {
                            SearchDo searchDo;
                            for (AutoMatchBo autoMatchBo : temp) {
                                searchDo = new SearchDo(type, AppConstant.CityCode);
                                searchDo.setTitle(autoMatchBo.getTagName());
                                searchDo.setCode(autoMatchBo.getTagCode());
                                searchDo.setCategory(autoMatchBo.getTagCategory());
                                searchList.add(searchDo);
                            }
                            searchAdapter.notifyDataSetChanged();
                            rv_search.setVisibility(View.VISIBLE);
                            showHistoryLayout(false);
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (fromUser)
                            netWorkUnable();
                    }
                });
        switch (type) {
            case 30:
                autoMatchApi.setType("r");
                break;
            case 60:
                autoMatchApi.setType("r");
                break;
            default:
                autoMatchApi.setType("s");
                break;
        }
    }

    @Override
    protected DefaultHint getDefaultHint() {
        getHistory(true);
        return DefaultHint.SALE;
    }

    @Override
    protected void onItemClick(SearchDo searchDo, boolean insert) {
        super.onItemClick(searchDo, insert);
        String category = searchDo.getCategory();
        if (type == 60 &&
                category.startsWith("est")) {//小区
            startActivity(new Intent(this, EsfEstateDetailActivity.class)
                    .putExtra(ExtraConstant.CestCode, searchDo.getCode()));
            return;
        } else if ("key".equals(searchDo.getCategory())) {
            Intent intent = new Intent();
            intent.putExtra(ExtraConstant.SearchContent, searchDo.getTitle());
            setResult(RESULT_OK, intent);
            finish();
            return;
        }
        Intent intent = new Intent();
        intent.putExtra(ExtraConstant.SearchObject, searchDo);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        super.onQueryTextSubmit(query);
        fromUser = true;
        if (type == 60) {
            if (TextUtils.isEmpty(query)) {
                rv_search.setVisibility(View.GONE);
                showHistoryLayout(true);
            } else {
                autoMatchApi.setKey(query);
                autoMatchApi.startRequest();
            }
        } else {
            Intent intent = new Intent();
            intent.putExtra(ExtraConstant.SearchContent, query);
            setResult(RESULT_OK, intent);
            finish();
        }
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        fromUser = false;
        if (TextUtils.isEmpty(newText)) {
            rv_search.setVisibility(View.GONE);
            showHistoryLayout(true);
        } else {
            autoMatchApi.setKey(newText);
            autoMatchApi.startRequest();
        }
        return true;
    }
}
